package edu.spbu.eclipse.xmledit.golubevhritoshin.editors;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class SXESyntaxErrors implements ErrorHandler {
	private IFile file;
	private boolean markers;

	public void removeAllMarkers() {
		try {
			file.deleteMarkers(IMarker.PROBLEM, true,
					IFile.DEPTH_INFINITE);
			markers = false;
		} catch (CoreException e) {
		}
	}

	public SXESyntaxErrors(IFile file) {
		this.file = file;
	}
	
	public void error(SAXParseException saxEx) throws SAXException {
		try {
			IMarker errorMarker = file.createMarker(IMarker.PROBLEM);
			errorMarker.setAttribute(IMarker.LINE_NUMBER, saxEx.getLineNumber());
			errorMarker.setAttribute(IMarker.MESSAGE, saxEx.getMessage());
			errorMarker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
			errorMarker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
			if (!markers) {
				markers = true;
			}
		} catch (CoreException e) {
		}
	}

	public void fatalError(SAXParseException saxEx) throws SAXException {
		try {
			IMarker errorMarker = file.createMarker(IMarker.PROBLEM);
			errorMarker.setAttribute(IMarker.LINE_NUMBER, saxEx.getLineNumber());
			errorMarker.setAttribute(IMarker.MESSAGE, saxEx.getMessage());
			errorMarker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
			errorMarker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
			if (!markers) {
				markers = true;
			}
		} catch (CoreException e) {
		}
	}

	public void warning(SAXParseException arg0) throws SAXException {
		try {
			IMarker errorMarker = file.createMarker(IMarker.PROBLEM);
			errorMarker.setAttribute(IMarker.LINE_NUMBER, arg0.getLineNumber());
			errorMarker.setAttribute(IMarker.MESSAGE, arg0.getMessage());
			errorMarker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_WARNING);
			errorMarker.setAttribute(IMarker.PRIORITY, IMarker.PRIORITY_HIGH);
		} catch (CoreException e) {
		}
	}
}
